package com.guigui.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.Query;
import com.guigui.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.guigui.gulimall.product.dto.AttrRelationAddDto;
import com.guigui.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.guigui.gulimall.product.service.AttrAttrgroupRelationService;
import com.guigui.gulimall.product.vo.AttrRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );
        return new PageUtils(page);
    }


    /**
     * 这里测试下xml效果，所以调用了Dao来做处理，没有用plus插件生成查询sql
     * @param attrRelationVos
     */
    @Override
    public void deleteAttrBatch(List<AttrRelationVo> attrRelationVos) {
        List<AttrAttrgroupRelationEntity> collect = attrRelationVos.stream().map((attrRelationVo) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrRelationVo, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationDao.deleteAttrBatch(collect);
    }

    @Override
    public void saveRelationBatch(List<AttrRelationAddDto> attrRelationAddDtos) {
        // 这些需要update操做
        List<Long> attrIdCollects = attrRelationAddDtos.stream().map(AttrRelationAddDto::getAttrId).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> queryRelationEntities = this.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_id", attrIdCollects));

        List<AttrAttrgroupRelationEntity> relationEntities = attrRelationAddDtos.stream().map(dto -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(dto, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        relationEntities.forEach(r-> {
            // 只会筛选出一条记录
            List<AttrAttrgroupRelationEntity> relation = queryRelationEntities.stream().filter(queryRelation -> r.getAttrId().equals(queryRelation.getAttrId())).collect(Collectors.toList());
            relation.forEach(e -> r.setId(e.getId()));
        });

        this.saveOrUpdateBatch(relationEntities);
    }

}